package com.sentiment.database.bean;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.bson.Document;
import org.bson.types.ObjectId;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sentiment.database.dbUtil.DbUtil;
import com.sentiment.tools.Format;

public class UserInfo{

	@Override
	public String toString() {
		return "UserInfo [_id=" + _id + ", sex=" + sex + ", birth=" + birth + ", phonenumb="
				+ phonenumb + ", email=" + email + ", idcardnumb=" + idcardnumb + ", user_id=" + user_id
				+ ", keywordList=" + keywordList + ", websiteList=" + websiteList + ", searchRecord_id="
				+ searchRecord_id + ", subscribeway=" + subscribeway + ", payment=" + payment + ",rigisterdate" + rigisterdate + "]";
	}

	public static final String dbName = "UserDB";
	public static final String collection = "UserInfo";
	
	private ObjectId _id;
	//用户性别
	private String sex;
	//用户出生日期
	private Date birth;
	//用户电话号码
	private String phonenumb;
	//用户邮箱
	private String email;
	//用户身份证号
	private String idcardnumb;
	//用户user表id
	private String user_id;
	//用户关注关键词
	private List<String> keywordList;
	//用户关注网站
	private List<String> websiteList;
	//用户关注查找id
	private List<String> searchRecord_id;
	//舆情订阅方式
	private String subscribeway;
	//账户余额
	private Double payment;
	//用户注册时间
	private String rigisterdate;
	
	
	public UserInfo(Document doc){
		super();
		_id = doc.getObjectId("_id");
		sex = doc.getString("sex");
		birth = doc.getDate("birth");
		phonenumb = doc.getString("phonenumb");
		email = doc.getString("email");
		idcardnumb = doc.getString("idcardnumb");
		user_id = doc.getString("user_id");
		keywordList = (List<String>) doc.get("keywordList");
		websiteList = (List<String>) doc.get("websiteList");
		searchRecord_id = (List<String>)doc.get("searchRecord_id");
		subscribeway = doc.getString("subscribeway");
		
		payment = doc.getDouble("payment");
		rigisterdate = doc.getString("rigisterdate");
		
	}
	
	public Document toDocument(){
		return new Document("sex", sex)
		.append("birth", birth).append("phonenumb", phonenumb)
		.append("email", email).append("idcardnumb", idcardnumb)
		.append("user_id", user_id)
		.append("keywordList", keywordList).append("websiteList", websiteList)
		.append("searchRecord_id", searchRecord_id).append("subscribeway", subscribeway)
		.append("payment", payment).append("rigisterdate", rigisterdate);
	}

	public UserInfo(String sex, Date birth, String phonenumb, String email,
			String idcardnumb, String user_id,
			List<String> keywordList, List<String> websiteList,
			List<String> searchRecord_id, String subscribeway, Double payment,
			String rigisterdate) {
		this.sex = sex;
		this.birth = birth;
		this.phonenumb = phonenumb;
		this.email = email;
		this.idcardnumb = idcardnumb;
		this.user_id = user_id;
		this.keywordList = keywordList;
		this.websiteList = websiteList;
		this.searchRecord_id = searchRecord_id;
		this.subscribeway = subscribeway;
		this.payment = payment;
		this.rigisterdate = rigisterdate;
	}

	public UserInfo() {
		// TODO Auto-generated constructor stub
	}
	@JsonIgnore
	public String get_id() {
		return _id.toString();
	}
	
	public void set_id(String _id) {
		this._id = new ObjectId(_id);
	}

	public String getSex() {
		if(sex.equals("female"))
			return "女";
		else
			return "男";
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getBirth() {
		if(birth!=null)
		return Format.date2String(birth,3);
		return null;
	}

	public void setBirth(Date birth) {
		this.birth = birth;
	}

	public String getPhonenumb() {
		return phonenumb;
	}

	public void setPhonenumb(String phonenumb) {
		this.phonenumb = phonenumb;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getIdcardnumb() {
		return idcardnumb;
	}

	public void setIdcardnumb(String idcardnumb) {
		this.idcardnumb = idcardnumb;
	}
	@JsonIgnore
	public String getUser_id() {
		return user_id;
	}
	
	public String getUsername() {
		
		User user = new User();
		user = user.getUserByUser_id(this.getUser_id());
		return user.getUsername();
	}
	
	public String getUsercode() {
		
		User user = new User();
		user = user.getUserByUser_id(this.getUser_id());
		return user.getUsercode();
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}
	
	
	public List<String> getRoleDetail() {
		
		
		User user = new User();
		List<Role> rolelist = user.getRolesByUser_id(this.getUser_id());
		List<String> roledetail = new ArrayList<>();
		for(int i = 0; i < rolelist.size(); i++){
			
			roledetail.add(rolelist.get(i).getRole_detail());
		}
		
		return roledetail;
	}

	
	@JsonIgnore
	public List<String> getKeywordList() {
		return keywordList;
	}

	public void setKeywordList(List<String> keywordList) {
		this.keywordList = keywordList;
	}
	
	@JsonIgnore
	public List<String> getWebsiteList() {
		return websiteList;
	}

	public void setWebsiteList(List<String> websiteList) {
		this.websiteList = websiteList;
	}
	@JsonIgnore
	public List<String> getSearchRecord_id() {
		return searchRecord_id;
	}

	public void setSearchRecord_id(List<String> searchRecord_id) {
		this.searchRecord_id = searchRecord_id;
	}
	
	public String getSubscribeway() {
		return subscribeway;
	}

	public void setSubscribeway(String subscribeway) {
		this.subscribeway = subscribeway;
	}

	public Double getPayment() {
		return payment;
	}

	public void setPayment(Double payment) {
		this.payment = payment;
	}
		
	public String getRigisterdate() {
		return rigisterdate;
	}

	public void setRigisterdate(String rigisterdate) {
		this.rigisterdate = rigisterdate;
	}
	
	public UserInfo getUserInfoByUser_id(String user_id) {
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		UserInfo info = new UserInfo(null, null, null, null, null, user_id, null, null, null, null, null, null);
		String[] keys = {"user_id"};
		Document t = info.toDocument();
		t = db.query(t, keys);
		
		db.closeConnection();
		
		if(t == null)
			return null;
		
		return new UserInfo(t);
	}
	

	public void test(){
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		List<String> users = new ArrayList<>();
		users.add("user");
		users.add("user_pay");
		users.add("admin");
		for(int j = 0; j < users.size(); j++){
			
			User user = new User();
			user = user.getUserByUserCode(users.get(j));
			String user_id = user.get_id();
			
			UserInfo info = new UserInfo("male", new Date(), "11111-111", "sndn@163.com", "510384746152630039", user_id, null, null, null, "邮箱推送", 34.5, Format.date2String(new Date(), 3));
			
			Document t = info.toDocument();
			db.insert(t);
		}
		
		
		db.closeConnection();
		
	}
	
	public static void main(String[] args) {
		
		
		UserInfo info = new UserInfo();
		info = info.getUserInfoByUser_id("56321e84c382362370407169");
		System.out.println(info.getSex());
	}

}
